package com.heaboy.feedback.websocket;


import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.AttributeKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.util.Date;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;


@Slf4j
@Component
@ChannelHandler.Sharable
public class WsContineHandler extends ChannelInboundHandlerAdapter {



    ConcurrentHashMap<Channel,String> channelMap = new ConcurrentHashMap<>();


    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
        if (msg instanceof FullHttpRequest){
            FullHttpRequest request = (FullHttpRequest) msg;

            String uri = request.uri();
            log.info(uri);
            String[] split = uri.split("/");
            String channeType = split[split.length - 1];
            log.info(channeType);
            channelMap.put(channelHandlerContext.channel(), channeType);
//
////            concurrentHashMap.put(courseId, nettyUser);
//            AttributeKey<String> key = AttributeKey.valueOf("courseId");
//            channelHandlerContext.attr(key).set(courseId);
//            // 在本channel上移除这个handler消息处理，即只处理一次，鉴权通过与否
            channelHandlerContext.pipeline().remove(WsContineHandler.class);
        }
        super.channelRead(channelHandlerContext, msg);
    }

    public boolean isTeacherChannel(Channel channel){
        return channelMap.getOrDefault(channel, "").equals("teacher");
    }
}
